﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DotNet.Business;
using DotNet.Common;
using DotNet.Model;
using System.Collections.Generic;
public partial class Admin_Default : basePage
{
    CompanyBll cBll = new CompanyBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CacheUtil.Remove("Company");
            Company cp = cBll.GetModel()[0];// (ViewState["dt"] as List<Company>)[0];
            if (cp == null)
            {
                this.TextBoxName.Text = "";
                this.TextBoxSheng.Text = "";
                this.TextBoxCity.Text = "";
                this.TextBoxaddress.Text = "";
                this.TextBoxcode.Text = "";
                this.Tel3.Text = "";
                this.TextBoxfax3.Text = "";
                this.TextBoxweb.Text = "";
                this.TextBox1type.Text = "";
                this.TextBox1date.Text = "";
                this.FCKeditor1.Value = "";
                DropDownListpeople.SelectedIndex = 0;
                DropDownListmoney.SelectedIndex = 0;
                this.Imagelogo.ImageUrl = "";
                this.Imagefac.ImageUrl = "";
            }
            else
            {
                this.TextBoxName.Text = cp.companyName.ToString();
                this.TextBoxSheng.Text = cp.Province.ToString();
                this.TextBoxCity.Text = cp.City.ToString();
                this.TextBoxaddress.Text =Convert.ToString(cp.CompanyAddress);
                this.TextBoxcode.Text = cp.Zipcode.ToString();
                this.Tel3.Text = cp.Tel.ToString();
                this.TextBoxfax3.Text = cp.Fax.ToString();
                this.TextBoxweb.Text = cp.Website.ToString();
                this.TextBox1type.Text =cp.CompanyType.ToString();
                this.TextBox1date.Text = cp.CompanyDate.ToString();
                this.TextBox1hangye.Text = cp.Trade.ToString();
                this.FCKeditor1.Value = cp.companyInfo.ToString();
                DropDownListpeople.SelectedValue=cp.People;
                DropDownListmoney.SelectedValue = cp.Turnover.ToString();
                this.Imagelogo.ImageUrl ="~"+cp.Logo;
                this.Imagefac.ImageUrl = "~"+cp.companyImg;
                ViewState["id"] = cp.companyId;
            }
            
        }
    }



  
    protected void CompanyBtn_Click1(object sender, EventArgs e)
    {
        try
        {
            Company cp = new Company();
            cp.companyName = this.TextBoxName.Text;
            cp.Province = this.TextBoxSheng.Text;
            cp.City = this.TextBoxCity.Text;
            cp.CompanyAddress = this.TextBoxaddress.Text;
            cp.Zipcode = this.TextBoxcode.Text;
            cp.Tel = this.Tel3.Text;
            cp.Fax = this.TextBoxfax3.Text;
            cp.Website = this.TextBoxweb.Text;
            cp.CompanyType = this.TextBox1type.Text;
            cp.CompanyDate = Convert.ToDateTime(this.TextBox1date.Text);
            cp.companyInfo = this.FCKeditor1.Value;
            cp.CompanyAddress = this.TextBoxaddress.Text;
            cp.People = DropDownListpeople.SelectedValue;
            cp.Turnover = DropDownListmoney.SelectedValue;
            cp.Trade = this.TextBox1hangye.Text;
            cp.companyId = Convert.ToInt32(ViewState["id"]);
            #region 上传
            DotUpload up = new DotUpload();
            up.AllowFileExtens = "gif|jpg|bmp|png";
            up.FilePath = "~/UploadFiles/CompanyProduct";
            up.MaxFileSize = 500;
            HttpPostedFile logo = FileUploadlogo.PostedFile;
            HttpPostedFile fac = filefac1.PostedFile;
            #endregion
            if (FileUploadlogo.HasFile)
            {
                if (up.DoUpload(logo, "zr"))
                    cp.Logo = up.ResultFileName;
            }
            else
            {
                cp.Logo = this.Imagelogo.ImageUrl.ToString().Replace("~", "");
            }
            if (filefac1.HasFile)
            {
                if (up.DoUpload(fac, "zr"))
                    cp.companyImg = up.ResultFileName;
            }
            else
            {
                cp.companyImg = this.Imagefac.ImageUrl.ToString().Replace("~", "");
            }

            if (cBll.Update(cp))
            {
                JsUtil.ShowMsg("保存成功", "?");
            }
            else
            {
                JsUtil.ShowMsg("错误");
                CacheUtil.Remove("Company");
            }
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}
